package com.ruoyi.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Arrays;

@Slf4j
@Component
public class StaticResourceConfig {

    private static Environment environment;

    public StaticResourceConfig() {
        this.environment = ApplicationContextHolder.getBean(Environment.class);
    }

    @PostConstruct
    public static void checkProfile() {
        String[] activeProfiles = environment.getActiveProfiles();
        if (activeProfiles.length == 0) {
            String defaultProfile = environment.getDefaultProfiles()[0];
            log.info("当前无激活Profile，使用默认: " + defaultProfile);
        } else {
            log.info("当前激活Profile: " + Arrays.toString(activeProfiles));
        }

        // 获取具体属性
        String profileFromEnv = environment.getProperty("spring.profiles.active");
        log.info("从Environment获取: " + profileFromEnv);
    }

    public static String getStaticRootPath() {
        //String[] profiles = environment.getActiveProfiles();
        String staticPath;
        String activeProfile = environment.getProperty("spring.profiles.active", "dev");
        if (activeProfile.equals("prod")) {
            staticPath = environment.getProperty("app.static.external-path", "./config/static/");
        } else {
            staticPath = environment.getProperty("app.static.dev-path", "ruoyi-cpadmin/src/main/resources/static/");
        }
        return staticPath;
    }
}
